package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.pojo.User;
import cn.com.dcsgo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

/**
 * UserController
 *
 * @author Dcsgo
 * @since 2025/2/20 9:51
 */
@RestController("user")
@Tag(name = "UserController")
public class UserController {
    private final UserService userService;

    @PostMapping("/login")
    @Operation(summary = "登录", description = "用户登录")
    public Result<User> login(@RequestBody User user) {
        User userResult = userService.login(user.getUsername(), user.getPassword());
        Result<User> result = new Result<>();
        if (userResult != null
                && userResult.getUsername() != null
                && !"".equals(userResult.getUsername())) {
            result.setCode(0);
            result.setMsg("登录成功");
            result.setData(userResult);
        } else {
            result.setCode(1);
            result.setMsg("登录失败");
        }
        return result;
    }

    @PostMapping({"/register"})
    @Operation(summary = "用户注册", description = "添加用户，自动生成自增长主键id并返回")
    public Result<Long> addUser(@RequestBody User user) {
        Result<Long> result = new Result<>();
        if (userService.addUser(user)) {
            result.setCode(0);
            result.setMsg("注册成功");
            result.setData(user.getId());
        } else {
            result.setCode(1);
            result.setMsg("注册失败");
        }
        return result;
    }

    @DeleteMapping
    @Operation(summary = "删除用户", description = "通过用户id删除用户")
    public Result<?> removeUser(@RequestParam Long id) {
        Result<?> result = new Result<>();
        if (userService.removeById(id)) {
            result.setCode(0);
            result.setMsg("删除成功");

        } else {
            result.setCode(1);
            result.setMsg("删除失败");
        }
        return result;
    }

    @GetMapping
    @Operation(summary = "查询用户", description = "通过用户id查询用户信息")
    public Result<User> queryUserById(@RequestParam Long id) {
        Result<User> result = new Result<>();
        User resultUser = userService.queryById(id);
        if (resultUser != null) {
            result.setCode(0);
            result.setMsg("查询成功");
            result.setData(resultUser);

        } else {
            result.setCode(1);
            result.setMsg("查询失败");
        }
        return result;
    }

    @PutMapping
    @Operation(summary = "更新用户信息", description = "通过用户id更新用户信息")
    public Result<User> updateUser(@RequestBody User user) {
        Result<User> result = new Result<>();
        User resultUser = userService.updateUser(user);
        if (resultUser != null) {
            result.setCode(0);
            result.setMsg("更新成功");
            result.setData(resultUser);

        } else {
            result.setCode(1);
            result.setMsg("更新失败");
        }
        return result;
    }

    public UserController(UserService userService) {
        this.userService = userService;
    }
}
